const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');

//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    latitude:'',
    pages:1,
    longitude: '',
    usertx: [],
    ys: '',
    th: '',
    imgUrls: [
    
    ],
    channel: [{
      iconUrl: 'https://ganhua-commodity.oss-cn-chengdu.aliyuncs.com/b_4057887_201710131432575637.png',
        name: '拼团',
        url: '../Assemble/Assemble'
      },
      {
        iconUrl: 'https://ganhua-commodity.oss-cn-chengdu.aliyuncs.com/timg%20%287%29.jpg',
        name: '供货商',
        url: '../ucenter/Supplier/Supplier'
      },
      {
        iconUrl: 'https://ganhua-commodity.oss-cn-chengdu.aliyuncs.com/24969966_205136788037_2.jpg',
        name: '电话',
        url: '../ucenter/phone/phone'
      },
      {
        iconUrl: 'https://ganhua-commodity.oss-cn-chengdu.aliyuncs.com/919dc292de75415c83133f88413c9e0c_th.jpg',
        name: '团长',
        url: '../ucenter/group/group'
      },
      {
        iconUrl: 'https://ganhua-commodity.oss-cn-chengdu.aliyuncs.com/5b316ad72f861_610.jpg',
        name: '我们',
        url: '../ucenter/about/about'
      }
      // ,
      // {
      //   iconUrl: 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
      //   name: '但是'
      // }
    ],


    text: "温馨提示：小程序拼团正在完善，请谨慎使用，为你带来的不变，请多谅解。",
    marqueePace: 1, //滚动速度
    marqueeDistance: 0, //初始滚动距离
    marquee_margin: 30, //间距
    size: 14, //字号
    time: '', // 定时器




    channell: [
    ],
    add:[],
    tz: {}
      // img: '../../static/images/timg.jpg',
      // name: '增勤',
      // store: '增勤的小店',
      // adress: '阿大会上登记卡的会计案例卡掉了卡里克'
    ,
    goods: [
     
    ],
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  jiazai(){
   
  },
  onPullDownRefresh: function () {
      this.onLoad()
  },
  onShareAppMessage: function() {
    return {
      title: '惠民惠到家',
      desc: '惠民惠到家',
      path: '/pages/index/index'
    }
  },
  onLoad: function(options) {
    this.getguanggao()
    this.getshop()
    var time = util.getTime(new Date())
    var time1 = util.getTomorrTime(new Date())
    this.setData({
      ys: time,
      th: time1
    })
    this.queryUserComm()
    let that=this
    wx.getLocation({
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        wx.setStorageSync('latitude', res.latitude)
        wx.setStorageSync('longitude', res.longitude)
        that.allTz()
      },
    })
  },
//拼团
  getshop2() {
    let that = this
    util.request(api.QueryGroupList, {}, 'post', {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    }).then(function (res) {
      console.log(res)
      that.setData({
        channell: res.data.groupList
      });
    });
  },


  getshop() {
    let that = this
    util.request(api.QueryGoods+'?page='+that.data.pages, {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
      'X-Qxmall-Token': wx.getStorageSync('token')
    }).then(function(res) {
      console.log(res)
      // var bb = bindscrolltolower
      if(res.data.goods.length==0){
        that.setData({
         isLoad:true
        })
      }
      var newArr = that.data.goods.concat(res.data.goods);
      that.setData({
        goods: newArr
      });
    });
  },
  getguanggao() {
    let that = this
    util.request(api.QueryAdvert, {

    }).then(function(res) {
      console.log(res)
      that.setData({
        imgUrls: res.data.noticeList
      });
    });
  },
  onHide: function() {
    //清空滚动字幕定时器，避免锁屏或页面隐藏后移动速度越来越快
    clearInterval(this.data.time);
  },
  gomore() {
    wx.navigateTo({
      url: '../Assemble/Assemble',
    })
  },
  onShow: function(e) {
   
    var that = this;
    var length = that.data.text.length * that.data.size; //文字长度
    var windowWidth = wx.getSystemInfoSync().windowWidth; // 屏幕宽度
    //console.log(length,windowWidth);
    that.setData({
      length: length,
      windowWidth: windowWidth
    });
    that.scrolltxt(); // 第一个字消失后立即从右边出现
    that.tzresult();
    that.getshop2()
  },
  //购物车
  add(e) {
    console.log(e.currentTarget.dataset.index)
    console.log(this.data.goods[0])
    let bb = e.currentTarget.dataset.index
    let that = this
    if (app.globalData.hasLogin == true) {
      //  if (that.data.num < 1) {
      // that.setData({
      //   num: that.data.num + 1
      // })
      var token = wx.getStorageSync('token')
      util.request(api.Addcart, {
        commodityId: that.data.goods[bb].id,
        number: 1,
        commImg: that.data.goods[bb].imgUrl,
        price: that.data.goods[bb].sellingPrice,
        commodityName: that.data.goods[bb].name,
        addTime: '',
        updateTime: '',
      }, 'post', {
          'Content-Type': 'application/json',
          'X-Qxmall-Token': wx.getStorageSync('token')
        }).then(function (res) {
          if (res.code == 200) {
            wx.showToast({
              title: '添加成功',
              icon: 'none'
            })
          }
          if (res.code == 405) {
            wx.showToast({
              title: '库存不足',
              icon: 'none'
            })
          }
          console.log(res)
        });

      // }else{
      //   util.showErrorToast('购物车已有该商品');
      // }

    } else {
      wx.navigateTo({
        url: '../login/login',
      })
    }

  },
//团长选择
  tzresult() {
    this.setData({
      add:[]
    })
    var name=wx.getStorageSync('name')
    if (wx.getStorageSync('name')){
      let that = this
      util.request(api.NearGroup, {
        name: name
      }, 'post', {
          'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
          'X-Qxmall-Token': wx.getStorageSync('token')
        }).then(function (res) {
          that.setData({
            tz: res.data.leaderList
          })
        })}
   
  },
//虚拟销量
  queryUserComm(){
    let that=this
    util.request(api.QueryUserComm, {
    }, 'get', {
      }).then(function (res) {
        console.log(res)
        that.setData({
          usertx: res.data.user
        })
       
      });
  },

  selecttz() {
    wx.navigateTo({
      url: '../ neartz/ neartz',
    })
  },

  allTz() {
    let that = this
    console.log("saasasas" + that.data.latitude)
    util.request(api.QueryLatelyLeader, {
      lat: that.data.latitude,
      lng: that.data.longitude
    }, 'post', {
        'Content-Type': 'application/json;charset=UTF-8'
      }).then(function (res) {
        console.log(res)
        if (res.code == 200) {
          console.log(res)
          wx.setStorageSync('name', res.data.leader.leaderName)
          that.setData({
            tz: res.data.leader
          })
        }
      });
  },


  /**
   * 滚动字幕
   */
  scrolltxt: function() {
    var that = this;
    var length = that.data.length; //滚动文字的宽度
    var windowWidth = that.data.windowWidth; //屏幕宽度
    var time = this.data.time
    if (length > windowWidth) {
      time = setInterval(function() {
        var maxscrollwidth = length + that.data.marquee_margin; //滚动的最大宽度，文字宽度+间距，如果需要一行文字滚完后再显示第二行可以修改marquee_margin值等于windowWidth即可
        var crentleft = that.data.marqueeDistance;
        if (crentleft < maxscrollwidth) { //判断是否滚动到最大宽度
          that.setData({
            marqueeDistance: crentleft + that.data.marqueePace
          })
        } else {
          that.setData({
            marqueeDistance: 0 // 直接重新滚动
          });
          clearInterval(time);
          that.scrolltxt();
        }
      }, 20);
    } else {
      that.setData({
        marquee_margin: "1000"
      }); //只显示一条不滚动右边间距加大，防止重复显示
    }
    that.setData({
      time: time
    })
  },
  onReachBottom: function () {
    this.setData({
      pages: this.data.pages + 1
    })
    this.getshop()
    console.log(this.data.pages)
  }
})